package com.eve.eplus.main;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.eve.eplus.R;
import com.eve.eplus.common.activity.BaseActivity;
import com.eve.eplus.common.adapter.IntroducePageAdapter;
import com.eve.eplus.common.app.Constants;

/**
 * 介绍页
 * 
 * @author norsie
 * @create 13-04-15
 */
public class IntroduceActivity extends BaseActivity
{
    private static final int TO_THE_END = 0;// 到达最后一张
    private static final int LEAVE_FROM_END = 1;// 离开最后一张

    /**
     * 介绍图片集合
     */
    private int[] ids = { R.drawable.welcome1, R.drawable.welcome2,
            R.drawable.welcome3, R.drawable.welcome4, R.drawable.bg_blue };

    private List<View> guides = new ArrayList<View>();
    private ViewPager pager;
    private ImageView open;
    private ImageView curDot;
    private int offset;// 位移量
    private int curPos = 0;// 记录当前的位置

    private Context mComtext = IntroduceActivity.this;
    private String source;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_introduce_main);
        source = getIntent().getStringExtra(Constants.ACTIVITY_SOURCE);

        for (int i = 0; i < ids.length; i++)
        {
            ImageView iv = new ImageView(this);
            iv.setImageResource(ids[i]);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                                                                       ViewGroup.LayoutParams.MATCH_PARENT,
                                                                       ViewGroup.LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(params);
            iv.setScaleType(ScaleType.FIT_XY);
            guides.add(iv);
        }

        curDot = (ImageView) findViewById(R.id.cur_dot);
        open = (ImageView) findViewById(R.id.open);
        curDot.getViewTreeObserver().addOnPreDrawListener(
                new OnPreDrawListener() {
                    public boolean onPreDraw()
                    {
                        offset = curDot.getWidth();
                        return true;
                    }
                });

        IntroducePageAdapter adapter = new IntroducePageAdapter(guides);
        pager = (ViewPager) findViewById(R.id.contentPager);
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new OnPageChangeListener() {
            public void onPageSelected(int arg0)
            {
                moveCursorTo(arg0);
                if (arg0 == ids.length - 1)
                {// 到最后一张了
                    handler.sendEmptyMessageDelayed(TO_THE_END, 500);
                }
                else if (curPos == ids.length - 1)
                {
                    handler.sendEmptyMessageDelayed(LEAVE_FROM_END, 100);
                }
                curPos = arg0;
            }

            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
            }

            public void onPageScrollStateChanged(int arg0)
            {
            }

        });
    }

    /**
     * 移动指针到相邻的位置
     * 
     * @param position
     *            指针的索引值
     * */
    private void moveCursorTo(int position)
    {
        TranslateAnimation anim = new TranslateAnimation(offset * curPos,
                                                         offset * position, 0,
                                                         0);
        anim.setDuration(300);
        anim.setFillAfter(true);
        curDot.startAnimation(anim);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg)
        {
            if (msg.what == TO_THE_END)
            {
                open.setVisibility(View.VISIBLE);
                open.setOnClickListener(onClick);
                open.setOnTouchListener(new OnTouchListener() {
                    public boolean onTouch(View v, MotionEvent event)
                    {
                        if (event.getAction() == MotionEvent.ACTION_DOWN)
                        {
                            v.setBackgroundResource(R.drawable.icon_goto_down);
                        }
                        else if (event.getAction() == MotionEvent.ACTION_UP)
                        {
                            v.setBackgroundResource(R.drawable.icon_goto);
                        }
                        return false;
                    }
                });
            }
            else if (msg.what == LEAVE_FROM_END)
                open.setVisibility(View.GONE);
        }
    };

    private View.OnClickListener onClick = new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.open:
                    if (source != null)
                    {
                        if (source.equals(Constants.ACTIVITY_SOURCE_SETACTIVITY))
                            onBackPressed();
                    }
                    else
                    {
                        gotoActivityAndFinish(mComtext, BindingActivity.class);
                    }
                    break;
            }
        }
    };
}
